<?php

namespace backend\modules\home\models;

use Yii;
use yii\behaviors\TimestampBehavior;
/**
 * This is the model class for table "lw_qikan".
 *
 * @property int $id
 * @property string $title
 * @property string $intro
 * @property int $order
 * @property string $image_path
 * @property string $keyword
 * @property string $desc
 * @property string $content
 * @property int $status
 * @property int $created_at
 * @property int $updated_at
 */
class Qikan extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'lw_qikan';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['intro', 'content'], 'string'],
            [['order', 'created_at', 'updated_at', 'status'], 'integer'],
            [['title', 'keyword', 'desc'], 'string', 'max' => 128],
            [['image_path'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'title' => '标题',
            'intro' => '简介',
            'order' => '排序',
            'image_path' => '封面图片',
            'keyword' => '关键词',
            'desc' => '描述',
            'content' => '期刊详情',
            'status' => '状态',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
        ];
    }

    public function behaviors()
    {
        return [
            [
                'class' => TimestampBehavior::className(),
                'createdAtAttribute' => 'created_at',
                'updatedAtAttribute' => 'updated_at',
                //'value' => new Expression('NOW()'),
                //'value'=>$this->timeTemp(),
            ],
        ];
    }

    public function beforeSave($insert)
    {
        if(parent::beforeSave($insert)){
            if($this->isNewRecord){
                if(!$this->order){
                    $this->order = 0;
                }
            }
            return true;
        }
        return false;
    }
}
